import Axios from 'axios'
import store from '@/store'
import JSONBIG from 'json-bigint'

const request = Axios.create({
  baseURL: 'http://toutiao-app.itheima.net', // 基础路径
  timeout: 3000,
  transformResponse: [function (data) {
    // 对 data 进行任意转换处理
    try {
      return JSONBIG.parse(data)
    } catch (_) {
      return data
    }
  }]
})

request.interceptors.request.use((config) => {
  if (store.state.user) {
    config.headers.Authorization = 'Bearer ' + store.state.user.token
  }

  return config
})

request.interceptors.response.use(res => {
  return res.data.data
})

export default request
